package com.company.project.service.impl;

import com.company.project.dao.AuthorityMapper;
import com.company.project.dao.NotificationMapper;
import com.company.project.dao.ThesisMapper;
import com.company.project.model.*;
import com.company.project.service.AuthorityService;
import com.company.project.core.AbstractService;
import org.hibernate.validator.internal.xml.GetterType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class AuthorityServiceImpl extends AbstractService<Authority> implements AuthorityService {
    @Resource
    private AuthorityMapper authorityMapper;

    @Resource
    private NotificationMapper notification;
    @Resource
    private ThesisMapper thesisMapper;

    @Override
    public List<Authority> selectAction(String name, String pwd) {
        List<Authority> authorities = authorityMapper.selectAction(name, pwd);
        List<Authority> auList = new ArrayList<>();
        Map<Integer, Authority> map = new HashMap<>();
        for (Authority authority : authorities) {
            if (authority.getParentid() == 0) {
                if (authority.getTitle().equals("消息通知")) {
                    Integer integer = notification.selectMessageQuantity(name, pwd);
                    if (integer == 0) {
                        authority.setMessageQuantity(0);
                    } else {
                        authority.setMessageQuantity(integer);
                    }
                }
                map.put(authority.getId(), authority);
            } else {
                if (authority.getTitle().equals("待处理稿件")) {
                    Integer integer = thesisMapper.selectPendingNum();
                    if (integer == 0) {
                        authority.setMessageQuantity(0);
                    } else {
                        authority.setMessageQuantity(integer);
                    }
                }
                map.get(authority.getParentid()).getAuList().add(authority);
            }
        }
        for (Map.Entry<Integer, Authority> entry : map.entrySet()) {
            auList.add(entry.getValue());
        }
        for (Authority n : auList) {
            if (n.getAuList().size() == 0) {
                n.setAuList(null);
            } else {
                for (Authority s : n.getAuList()) {
                    s.setAuList(null);
                }
            }
        }
        return auList;
    }

    /**
     * 删除权限
     *
     * @param rid
     * @return
     */
    @Override
    public Boolean deleteAuthority(Integer rid, Integer parentid) {
        return authorityMapper.deleteAuthority(rid, parentid);
    }

    /**
     * 删除后更新
     * @param roleAuthority
     * @return
     */
    @Override
    public Boolean upAuthority(RoleAuthority roleAuthority) {
        return authorityMapper.upAuthority(roleAuthority);
    }

    @Override
    public Boolean addAuthority(RoleAuthority roleAuthority) {
        return authorityMapper.addAuthority(roleAuthority);
    }

    /**
     * 查询添加权限的所有权限
     * @return
     */
    @Override
    public List<Authority> selectAuthorityAll() {
        return authorityMapper.selectAuthorityAll();
    }

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Authority> Function() {
        return authorityMapper.Function();
    }

    @Override
    public List Submenu(String id) {
        return authorityMapper.Submenu(id);
    }


}
